package com.jieli.bt.att.ui.widget.decoration

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.jieli.bt.att.util.ViewUtil

/**
 * CommonItemDecoration
 * @author zqjasonZhong
 * @since 2024/7/31
 * @email zhongzhuocheng@zh-jieli.com
 * @desc 通用分隔符
 */
class CommonItemDecoration(
    private val context: Context,
    private val divider: Drawable,
    private val orientation: Int = RecyclerView.VERTICAL,
    private val dividerHeight: Int = getDefaultHeight(context)
) :
    RecyclerView.ItemDecoration() {
    companion object {
        private val attrs = intArrayOf(android.R.attr.listDivider)

        private fun getDefaultHeight(context: Context): Int = ViewUtil.dp2px(context, 1)

        private fun getDefaultDrawable(context: Context): Drawable {
            val typedArray = context.obtainStyledAttributes(attrs)
            val drawable = typedArray.getDrawable(0)!!
            typedArray.recycle()
            return drawable
        }
    }

    constructor(context: Context, orientation: Int) : this(
        context,
        getDefaultDrawable(context),
        orientation
    )

    constructor(context: Context, color: Int, orientation: Int, dividerHeight: Int) : this(
        context,
        ColorDrawable(color),
        orientation,
        dividerHeight
    )


    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        drawHDecoration(c, parent)
        drawVDecoration(c, parent)
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
//        super.getItemOffsets(outRect, view, parent, state)
        if (RecyclerView.HORIZONTAL == orientation) {
            outRect.set(0, 0, dividerHeight, 0)
        } else {
            outRect.set(0, 0, 0, dividerHeight)
        }
    }

    private fun drawHDecoration(canvas: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val lp = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + lp.bottomMargin
            val bottom = top + dividerHeight
            divider.setBounds(left, top, right, bottom)
            divider.draw(canvas)
        }
    }

    private fun drawVDecoration(canvas: Canvas, parent: RecyclerView) {
        val top = parent.paddingTop
        val bottom = parent.height - parent.paddingBottom
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val lp = child.layoutParams as RecyclerView.LayoutParams
            val left = child.right + lp.rightMargin
            val right = left + dividerHeight
            divider.setBounds(left, top, right, bottom)
            divider.draw(canvas)
        }
    }
}